<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>isSorted</title>
</head>

<body>
    <script>
        const isSorted = arr => {
            if (!Array.isArray(arr) || arr.length <= 1) {
                return 0;
            }
            const direction = arr[1] - arr[0];
            for (let i = 2; i < arr.length; i++) {
                if ((arr[i] - arr[i - 1]) * direction < 0) {
                    return 0;
                }
            }
            return Math.sign(direction);
        };
        document.write(`[4, 3, 2]${isSorted([4, 3, 2]) ? '不' : ''}需要排序!`)
    </script>
</body>

</html>